package com.astronomy.article.service;

import com.astronomy.article.mapper.ChannelMapper;
import com.astronomy.article.pojo.Channel;
import com.astronomy.entity.IdWorker;
import com.astronomy.entity.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class ChannelService {

    @Autowired
    private ChannelMapper channelMapper;
    @Autowired
    private IdWorker idWorker;

    /**
     * 全部数据
     *
     * @return
     */
    public List<Channel> findAll() {
        return channelMapper.selectAll();
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    public Channel findById(String id) {
        return channelMapper.selectByPrimaryKey(id);
    }

    /**
     * 增加
     *
     * @param channel
     */
    public void add(Channel channel) {
        channel.setId(idWorker.nextId()+"");
        channelMapper.insert(channel);
    }

    /**
     * 修改
     *
     * @param channel
     */
    public void update(Channel channel) {
        channelMapper.updateByPrimaryKey(channel);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void delete(String id) {
        channelMapper.deleteByPrimaryKey(id);
    }


    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    public Page<Channel> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        return (Page<Channel>) channelMapper.selectAll();
    }

    /**
     * 条件查询
     *
     * @param searchMap
     * @return
     */
    public List<Channel> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return channelMapper.selectByExample(example);
    }

    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Channel.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // ID
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))) {
                criteria.andLike("id", "%" + searchMap.get("id") + "%");
            }
            // 频道名称
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            // 状态
            if (searchMap.get("state") != null && !"".equals(searchMap.get("state"))) {
                criteria.andLike("state", "%" + searchMap.get("state") + "%");
            }


        }
        return example;
    }

    /**
     * 条件+分页查询
     *
     * @param searchMap 查询条件
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    public PageResult<Channel> findPage(Map<String, Object> searchMap, int page, int size) {
        Example example = createExample(searchMap);
        PageHelper.startPage(page, size);
        List<Channel> channels = channelMapper.selectByExample(example);
        PageInfo<Channel> channelPageInfo = new PageInfo<>(channels);
        return new PageResult<>(channelPageInfo.getTotal(), channelPageInfo.getList());
    }

}
